<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>
          开票管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="ser.key" placeholder="开票名称" style="width: 260px" class="handle-input mr10" @keypress.enter="searchClick" clearable>
          <template #prepend>开票名称</template>
        </el-input>
        <el-date-picker v-model="ser.startTime" type="date" placeholder="开始日期" value-format="YYYY-MM-DD" class="handle-input mr10" clearable></el-date-picker>
        <el-date-picker v-model="ser.endTime" type="date" placeholder="结束日期" value-format="YYYY-MM-DD" class="handle-input mr10" clearable></el-date-picker>

        <el-button type="primary" icon="el-icon-search" @click.stop="searchClick">搜索</el-button>
        <el-button @click.stop="addClick">添加</el-button>
      </div>
      <el-table :data="datas" border class="table" ref="multipleTable" header-cell-class-name="table-header">
        <el-table-column prop="name" label="开票名称"></el-table-column>
        <el-table-column prop="invoiceDate" label="开票日期" :formatter="dateformatter" width="100"></el-table-column>
        <el-table-column prop="customerName" label="客户名称"></el-table-column>
        <el-table-column prop="invoiceTypeName" label="发票类型" width="200"></el-table-column>
        <el-table-column prop="money" label="开票金额" width="130" :formatter="(...arr) => arr[2] + '元'"></el-table-column>
        <el-table-column prop="tickerName" label="开票人" width="130"></el-table-column>
        <el-table-column prop="orgName" label="归属部门" width="130"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="text" icon="el-icon-view" @click="handleView(scope.row)">预览</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="ser.page" :page-size="ser.row" :total="total" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>
    <form-add ref="r_add" @search="search"></form-add>
    <form-view ref="r_view"></form-view>
  </div>
</template>
<script>
import FormAdd from './Add.vue'
import FormView from './View.vue'
export default {
  components: { FormAdd, FormView },
  data() {
    return {
      ser: {
        page: 1,
        row: 10,
        key: '',
        startTime: '',
        endTime: '',
      },
      datas: [],
      total: 0,
    }
  },
  methods: {
    addClick() {
      this.$refs.r_add.show()
    },
    searchClick() {
      this.handlePageChange(1)
    },
    handleView(row) {
      this.$refs.r_view.show(row.id)
      // console.log('产品预览', JSON.stringify(row))
    },
    handleDelete(row) {
      this.$confirm('是否确定要删除当前信息？')
        .then(() => {
          this.$api.invoice.remove(row.id).then(this.search)
        })
        .catch(() => {})
    },
    handlePageChange(page) {
      this.ser.page = page
      this.search()
    },
    search() {
      this.$api.invoice.search(this.ser).then(({ msg: { items, total } }) => {
        this.datas = items
        this.total = total
      })
    },
    dateformatter(...arr) {
      return arr[2].slice(0, 10)
    },
  },
  mounted() {
    this.searchClick()
  },
}
</script>
<style lang="scss" scoped></style>
